import 'package:flutter/material.dart';
import '../../utils/g.dart';
import '../../utils/payment.dart';

class OrderPay extends StatefulWidget {
  String uni;
  OrderPay({Key? key, required String this.uni }) : super(key: key);

  @override
  _OrderPayState createState() => _OrderPayState();
}

class _OrderPayState extends State<OrderPay> {
  Map order = {};
  var _futureInstance;

  @override
  void initState() { 
    super.initState();
    _futureInstance = G.api.order.detail(widget.uni);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('订单完成'),
        centerTitle: true,
      ),
      body: FutureBuilder(
        future: _futureInstance,
        builder: (context, snapshot) {
          if (snapshot.hasData) {
            var res = snapshot.data as Map;
            if (res['status'] == 200) {
              order = res['data'];

              return Container(
                color: Colors.grey.shade200,
                child: ListView(
                  children: [
                    _renderOrderDetail(),
                  ],
                ),
              );
            } else {
              // 报错
              G.error('查询订单详情失败');
            }
          } else if (snapshot.hasError) {
            return Center(
              child: Text("${snapshot.error}"),
            );
          }

          return G.loader();
        }
      )
    );
  }

  Widget _renderOrderDetail() {
    return Container(
      margin: const EdgeInsets.fromLTRB(10, 10, 10, 10),
      color: Colors.white,
      child: Column(
        children: [
          Container(
            padding: const EdgeInsets.fromLTRB(10, 0, 10, 30),
            child: Column(
              mainAxisAlignment: MainAxisAlignment.spaceAround,
              children: [
                Icon(
                  Icons.star, 
                  size: 70, 
                  color: Theme.of(context).primaryColor
                ),
                const Text(
                  "下单成功",
                  style: TextStyle(
                    fontSize: 26,
                    fontWeight: FontWeight.w600
                  )
                )
              ],
            ),
          ),
          ListTile(
            title: const Text('订单编号'),
            trailing: Text(order['order_id'].toString()),
          ),
          ListTile(
            title: const Text('下单时间'),
            trailing: Text(order['_add_time'].toString()),
          ),
          ListTile(
            title: const Text('支付方式'),
            trailing: Text(_getPaymentName()),
          ),
          ListTile(
            title: const Text('支付金额'),
            trailing: Text(order['pay_price'].toString()),
          ),

          // 查看订单详情
          _checkOrderDetail(widget.uni),

          // 返回首页
          _goHome()
        ],
      )
    );
  }

  String _getPaymentName() {
    String target = "";
    List payment = getPayment();
    for (var item in payment) {
      if (item['value'] == order['pay_type']) {
        target = item['name'];
      }
    }

    return target;
  }

  Widget _checkOrderDetail(String uni) {
    return Container(
      padding: const EdgeInsets.all(20),
      child: ElevatedButton(
        onPressed: () {
          // 跳转到订单详情页面
          Future.delayed(const Duration(milliseconds: 400), () {
            Map<String, String> p = {
              "uni": uni
            };
            G.router.navigateTo(context, '/order_detail'+G.parseQuery(params: p));
          });
        }, 
        child: const Text('查看订单'),
        style: ButtonStyle(
          elevation: MaterialStateProperty.all(10),
          shape: MaterialStateProperty.all(
            const StadiumBorder(
              side: BorderSide(
                color: Colors.blueAccent,
                width: 1
              )
            )
          ),
          minimumSize: MaterialStateProperty.all(Size(G.width - 40, 50))
        )
      ),
    );
  }

  Widget _goHome() {
    return Container(
      padding: const EdgeInsets.all(20),
      child: OutlinedButton(
        onPressed: () {
          // 跳转到首页
          G.router.navigateTo(context, '/');
        }, 
        child: const Text('回到首页'),
        style: ButtonStyle(
          elevation: MaterialStateProperty.all(10),
          backgroundColor: MaterialStateProperty.resolveWith((states) {
            // 设置按下时的背景色
            if (states.contains(MaterialState.pressed)) {
              return Colors.blue.shade200;
            } else {
              return Colors.white;
            }
          }),
          shape: MaterialStateProperty.all(
            const StadiumBorder(
              side: BorderSide(
                color: Colors.blueAccent,
                width: 1
              )
            )
          ),
          minimumSize: MaterialStateProperty.all(Size(G.width - 40, 50))
        )
      ),
    );
  }
}